﻿namespace ComTools.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// 这个类提供了处理系统类型的基本操作，这个主要用在query操作中
    /// </summary>
    public class TypeHelper
    {
        /// <summary>
        /// 数值类型
        /// </summary>
        private static readonly IDictionary<string, bool> _numericTypes;

        /// <summary>
        /// 基础类型
        /// </summary>
        private static readonly IDictionary<string, bool> _basicTypes;

        /// <summary>
        /// 静态初始化函数
        /// </summary>
        static TypeHelper()
        {
            _numericTypes = new Dictionary<string, bool>
            {
                [typeof(int).Name] = true,
                [typeof(long).Name] = true,
                [typeof(float).Name] = true,
                [typeof(double).Name] = true,
                [typeof(decimal).Name] = true,
                [typeof(sbyte).Name] = true,
                [typeof(Int16).Name] = true,
                [typeof(Int32).Name] = true,
                [typeof(Int64).Name] = true,
                [typeof(Double).Name] = true,
                [typeof(Decimal).Name] = true
            };

            _basicTypes = new Dictionary<string, bool>
            {
                [typeof(int).Name] = true,
                [typeof(long).Name] = true,
                [typeof(float).Name] = true,
                [typeof(double).Name] = true,
                [typeof(decimal).Name] = true,
                [typeof(sbyte).Name] = true,
                [typeof(Int16).Name] = true,
                [typeof(Int32).Name] = true,
                [typeof(Int64).Name] = true,
                [typeof(Double).Name] = true,
                [typeof(Decimal).Name] = true,
                [typeof(bool).Name] = true,
                [typeof(DateTime).Name] = true,
                [typeof(string).Name] = true
            };
        }


        /// <summary>
        /// 判断提供的值是否是一个数字类型
        /// </summary>
        /// <param name="val">要判断的对象</param>
        /// <returns>如果提供的对象是数值类型则返回true</returns>
        public static bool IsNumeric(object val)
        {
            if (val == null)
                return false;
            return _numericTypes.ContainsKey(val.GetType().Name);
        }


        /// <summary>
        /// 判断指定的类型是否是数值类型
        /// </summary>
        /// <param name="type">要判断的类型</param>
        /// <returns>如果提供的类型是数值类型则返回true</returns>
        public static bool IsNumeric(Type type)
        {
            return _numericTypes.ContainsKey(type.Name);
        }


        /// <summary>
        /// 判断指定的类型是否是基本类型
        /// </summary>
        /// <param name="type">要判断的类型</param>
        /// <returns>如果提供的类型是基本类型则返回true</returns>
        public static bool IsBasicType(Type type)
        {
            return _basicTypes.ContainsKey(type.Name);
        }


        /// <summary>
        /// 将一个对象的集合转换为以逗号分隔的字符串
        /// </summary>
        /// <param name="vals">对象的数组</param>
        /// <returns>以逗号分隔对象属性的字符串</returns>
        public static string Join(object[] vals)
        {
            StringBuilder buffer = new StringBuilder();

            // 获取当前数组装的数据是什么类型
            Type arrayType = vals[0].GetType();


            if (arrayType == typeof(int[]))
            {
                int[] iarray = vals[0] as int[];
                if (iarray == null) return buffer.ToString();
                buffer.Append(iarray[0].ToString());
                for (int ndx = 1; ndx < iarray.Length; ndx++)
                    buffer.Append(", " + iarray[ndx]);
            }

            else if (arrayType == typeof(long[]))
            {
                long[] iarray = vals[0] as long[];
                buffer.Append(iarray[0].ToString());
                for (int ndx = 1; ndx < iarray.Length; ndx++)
                    buffer.Append(", " + iarray[ndx]);
            }

            else if (arrayType == typeof(float[]))
            {
                float[] iarray = vals[0] as float[];
                buffer.Append(iarray[0].ToString());
                for (int ndx = 1; ndx < iarray.Length; ndx++)
                    buffer.Append(", " + iarray[ndx]);
            }

            else if (arrayType == typeof(double[]))
            {
                double[] iarray = vals[0] as double[];
                buffer.Append(iarray[0].ToString());
                for (int ndx = 1; ndx < iarray.Length; ndx++)
                    buffer.Append(", " + iarray[ndx]);
            }

            else
            {
                buffer.Append(vals[0].ToString());
                for (int ndx = 1; ndx < vals.Length; ndx++)
                    buffer.Append(", " + vals[ndx]);
            }
            return buffer.ToString();
        }
    }
}
